﻿//                       NOTICE
// 
// This software was produced for the U. S. Government
// under Contract No. FA8721-10-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)
// 
// � 2010 The MITRE Corporation. All Rights Reserved. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MXSE.MultiTouch.Gestures
{
    /// <summary>
    /// Abstract class for Gestures that use two or more contacts.
    /// </summary>
    public abstract class ComplexGesture : Gesture
    {
        /// <summary>
        /// List of BasicGesture children.
        /// </summary>
        private List<BasicGesture> basics;
        public IList<BasicGesture> Basics
        {
            get
            {
                return basics.AsReadOnly();
            }
        }

        /// <summary>
        /// Constructor just meant to initialize list of BasicGestures.
        /// </summary>
        public ComplexGesture()
        {
            basics = new List<BasicGesture>();
        }

        public void AddBasic(BasicGesture basic)
        {
            basic.Parent = this;
            basics.Add(basic);
        }

        /// <summary>
        /// Returns true if gesture has no children or
        /// it was set to be removed by the Destroy() method.
        /// </summary>
        /// <returns>True if gesture should be removed.</returns>
        public override bool Destroyed()
        {
            if (destroyed || basics.Count == 0 )
                return true;
            foreach (BasicGesture b in basics)
                if (b.Destroyed())
                    return true;
            return false;
        }

        protected override void OnDeath()
        {
            foreach (BasicGesture b in basics)
                b.Parent = null;
        }

        /// <summary>
        /// Runs through actions, then updates children.
        /// </summary>
        public override void Update()
        {
            base.Update();
            foreach (BasicGesture basic in basics)
                basic.Update();
        }
    }
}
